package com.xsl.culture.mybasevideoview.controller;


import com.facebook.stetho.okhttp3.StethoInterceptor;
import com.xapp.jjh.logtools.logger.LogUtil;
import com.xsl.culture.mybasevideoview.CustomApp;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.SSLSocketFactory;

import okhttp3.Cache;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;

public class OkHttpManager {
    private static final String TAG = "MycpApi";
    private static OkHttpClient mOkHttpClient;
    private static SSLSocketFactory socketFactory;
    //设置缓存目录
    private static File cacheDirectory = new File(CustomApp.get().getCacheDir().getAbsolutePath(), "MycpCache");
    private static Cache cache = new Cache(cacheDirectory, 10 * 1024 * 1024);
    /**
     * 获取OkHttpClient对象
     * @return
     */
    public static OkHttpClient getOkHttpClient() {
        if (null == mOkHttpClient) {
            //添加HttpLogging拦截器，方便观察，上传和返回json
//            HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
//            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            //同样okhttp3后也使用build设计模式
            mOkHttpClient = new OkHttpClient.Builder()
                    .addInterceptor(new LogInterceptor())
                    //设置一个自动管理cookies的管理器
//                    .cookieJar(new CookiesManager())
                    //添加网络连接器
                     .addNetworkInterceptor(new StethoInterceptor())
                    //设置请求读写的超时时间
                    .connectTimeout(30, TimeUnit.SECONDS)
                    .writeTimeout(30, TimeUnit.SECONDS)
                    .readTimeout(30, TimeUnit.SECONDS)
                    .cache(cache)
                    //设置https
//                        .hostnameVerifier(new HostnameVerifier() {
//                            @Override
//                            public boolean verify(String hostname, SSLSession session) {
//                                return true;
//                            }
//                        })
//                        .sslSocketFactory(setCertificates(App.getInstance().getAssets().open("https.cer")))
                    .build();
        }
        return mOkHttpClient;
    }

    // OkHttp3 的监听
    private static class LogInterceptor implements Interceptor {
        @Override
        public okhttp3.Response intercept(Chain chain) throws IOException {
            okhttp3.Response response = chain.proceed(chain.request());
            okhttp3.MediaType mediaType = response.body().contentType();
            String content = response.body().string();
            Request request = chain.request();
//            LogUtils.loge(String.format("Sending request %s on %s%n%s", request.url(),  chain.connection(), request.headers()));
            LogUtil.e(String.format("链接地址>>>>>%s", request.url()) + "\n"+content);
            return response.newBuilder().body(okhttp3.ResponseBody.create(mediaType, content)).build();
        }
    }
}
